package com.fhhst.prodroid;

import java.io.IOException;

import com.hoho.android.usbserial.driver.UsbSerialDriver;

/**
 * @author robert
 * 
 */
public class Printcore {

	private StringBuffer mainqueue;
	private StringBuffer priqueue;
	private StringBuffer sentlines; // Lines already Sent to printer
	private int lineno;
	private int queueindex;
	private int resendfrom;
	private boolean connected;
	private boolean online;
	private UsbSerialDriver mSerialDevice;
	private Printer lPrinter;

	public Printcore(Printer aPrinter) {
		mainqueue = new StringBuffer();
		priqueue = new StringBuffer();
		lineno = -1;
		queueindex = -1;
		resendfrom = -1;
		connected = false;
		online = false;
		lPrinter=aPrinter;
		
		if (lPrinter!=null) {
			connected=lPrinter.isConnected();	
		}
		
	}

	public void connect() {
		
		if (lPrinter.isConnected()) {
			disconnect();			
		}		
		
		enableListen();	
		
	}

	public void disconnect() {

	}

	public void pause() {
		
	}

	public void resume() { //resumeprint?
		
	}

	public void reset() {
		lPrinter.reset();
	}

	public void readline() { // ????

	}

	/**
	 * Enable the reading and decoding of messages from the printer
	 */
	public void enableListen() {

			if (connected) {
				while (!online) {
					
					
					// send m105 every 5 ticks
					
				
			}
		}
		// go to the main activity and send a M105
	}

	/**
	 * @param cmd
	 *            a N + Line Number
	 * @return checksum of a command according to http://reprap.org/wiki/G-code
	 */
	public int checksum(char[] cmd) {
		int cs = 0;
		for (int i = 0; (cmd[i] != '*') && (Character.isDefined(cmd[i])); i++)
			cs = cs ^ cmd[i];
		cs &= 0xff;
		return cs;
	}
}
